﻿using Sidvall.IO.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace Sidvall.Web.IO.Controllers
{
    public class FileSystemEntryController : ApiController
    {
        #region Public Members

        #region GetNames

        [HttpPost]
        [Route("api/V1/NetFileSystem/{StoreId}/FileSystemEntry/GetNames")]
        public async Task<HttpResponseMessage> GetNames(string storeId, [FromBody]GetNamesContext criteria)
        {
            HttpResponseMessage message;

            if (criteria == null)
                throw new System.ArgumentNullException("criteria");
            try
            {
                var result = await SystemContext.Current.FileSystemEntryManager.GetFileSystemEntryNamesAsync(storeId, criteria.Path, criteria.SearchPattern, criteria.SearchOption, CancellationToken.None);
                if ((result != null) && (result.Any()))
                    message = this.Request.CreateResponse(HttpStatusCode.OK, result.ToArray());
                else
                    message = this.Request.CreateResponse(HttpStatusCode.NoContent);
            }
            catch (System.Exception ex)
            {
                await SystemContext.Current.ControllerManager.LogAsync(ex);
                message = SystemContext.Current.ControllerManager.GetResponseMessage(this, ex);
            }
            return message;
        }

        #endregion

        #endregion
    }
}
